package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.link.utils.AddressHelper;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;
import com.sgb.jicai.entity.FactoryCentralizedListEntity;
import com.sgb.jicai.entity.JiCaiHomeListRequestBean;
import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.sgb.jicai.view.fragment.FactoryCentralizedFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FactoryCentralizedFragmentModel extends ViewModel {
    private  CommonPopEntity mAll;
    private FactoryCentralizedFragment fragment;
    private Map map;
    private int page = 1;

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
           // if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
           // }
        } else {
            DialogHelper.dismissProgressDialog();
            fragment.closeRecyclerRefreshing();
        }
    }

    //集采状态
    public void groupBuyClassList(int type, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        JiCaiBaseApi.getApi().getStatus().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                showOrHideDialog(false);
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().get(0).setNo(""); // 全部的key值为-1，
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                }
            }
        });
    }

    //集采时间
    public void showTimePopWindow(int type, boolean isShowLeftDialog) {
        JiCaiBaseApi.getApi().getDateTimeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                showOrHideDialog(false);
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, mAll);
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                }
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel(int type) {
        showOrHideDialog(true);
        AddressHelper.getInstance().getAllArea(new AddressHelper.AddressCallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                showOrHideDialog(false);
                if (data != null && data.size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data) {
                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    fragment.setChooseItemData(type, entities);
                    fragment.setAddressData(data);
                }
            }
        });
    }

    public void getAddressCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no, int type, boolean isShowLeftDialog) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> children = scrDatum.getChildren();
                if (children != null && children.size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : children) {
                        entities.add(new CommonPopEntity(child.getNo(), child.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, entities);
                    } else {
                        fragment.setChooseItemData(type, entities);
                    }
                }
            }
        }
    }

    //获取商品类型
    //二级分类
    public void getTenderGoodsCategoryAllList(int type, String no, boolean isShowLeftDialog) {
        showOrHideDialog(true);
       JiCaiBaseApi.getApi().getSecondCategory(no, "1").enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, mAll);
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                }
            }
        });
    }

    /**
     * 获取供方发起列表
     */
    public void getFactoryCentralizedList(JiCaiHomeListRequestBean map, boolean loadMore) {
        page = loadMore ? page + 1 : 1;
        map.setCurrent(page);
        map.setLimit(10);
        showOrHideDialog(true);
        JiCaiBaseApi.getApi().getFactoryCentralizedLists(map).enqueue(new ICallBack<BaseEntity<FactoryCentralizedListEntity<FactoryCentralizedItemEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<FactoryCentralizedListEntity<FactoryCentralizedItemEntity>> data) {
                showOrHideDialog(false);
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    int current = data.getData().getCurrent();
                    int total = data.getData().getTotal();
                    fragment.setJiCaiList(data.getData().getList(), loadMore, current >= total);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }


    public void getH5Url(String id) {
        showOrHideDialog(true);
        JiCaiBaseApi.getApi().getUrlSetting("Factory").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                showOrHideDialog(false);
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    public void setActivity(FactoryCentralizedFragment factoryCentralizedFragment) {
        this.fragment = factoryCentralizedFragment;
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
    }
}
